package ext;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.codehaus.plexus.archiver.jar.JarArchiver;

/**
 * 
 * @author 北京八维博大科技 薛建新
 * @date 2022年11月10日 13:58:05
 * @Copyright 北京八维博大科技
 */
public class CryptoClass {

	public static void main(String[] args) {
		doCryptoClass();
	}

	private static void doCryptoClass() {
		//
		String basePath = "D:/work20220906/java/myopen/springboot2/demo20220728test/target/classes/";
		String jarName = "demo20220728test-1.0.0.jar";
		try {

			// 加密并覆盖
//			Files.walk(Path.of(basePath,"com/xue/test/"),1)
//			.peek(p-> System.out.println(p.getFileName())) 
//			.filter(p->p.getFileName().toString().endsWith(".class"))
//			.peek(p-> System.out.println("-->"+p)) 
//			.forEach(p->{
//				try {
//					 Files.write(p, Crypto.encrypt(Crypto.key, Files.readAllBytes(p))) ;
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			});

			// 备份
			//Files.move(Path.of(basePath, "com/xue/test/Hello.class"), Path.of(basePath, "com/xue/test/Hello.class1"));
			//Files.move(Path.of(basePath, "com/xue/test/Hello1.class"), Path.of(basePath, "com/xue/test/Hello1.class1"));
			// 加密
			//Files.write(Path.of(basePath, "com/xue/test/Hello.class"),
			//		Crypto.encrypt(Crypto.key, Files.readAllBytes(Path.of(basePath, "com/xue/test/Hello.class1"))));
			//Files.write(Path.of(basePath, "com/xue/test/Hello1.class"),
			//		Crypto.encrypt(Crypto.key, Files.readAllBytes(Path.of(basePath, "com/xue/test/Hello1.class1"))));

			// 解密
			//Files.write(Path.of(basePath,"com/xue/test/Hello_decrypt.class"), 
			//		Crypto.decrypt(Crypto.key, Files.readAllBytes(Path.of(basePath,"com/xue/test/Hello.class")))) ;
			//Files.write(Path.of(basePath,"com/xue/test/Hello1_decrypt.class"), 
			//		Crypto.decrypt(Crypto.key, Files.readAllBytes(Path.of(basePath,"com/xue/test/Hello1.class")))) ;
			
			
			Files.write(Path.of("C:/Users/xueji/Desktop/Hello.class"), 
					Crypto.decrypt(Crypto.key, Files.readAllBytes(Path.of(basePath,"com/xue/test/Hello.class")))) ;
			Files.write(Path.of("C:/Users/xueji/Desktop//Hello1.class"), 
					Crypto.decrypt(Crypto.key, Files.readAllBytes(Path.of(basePath,"com/xue/test/Hello1.class")))) ;

			boolean needPackage = false;
			if (needPackage) {
				// 3.重新打包 打的包有问题
				JarArchiver jarArchiver = new JarArchiver();
				jarArchiver.setDestFile(new File("d:/" + jarName));
				jarArchiver.addDirectory(new File(basePath));
				jarArchiver.setCompress(false);// 重新打包不能压缩
				jarArchiver.setManifest(new File(basePath, "META-INF/MANIFEST.MF"));// 必须重新添加这个文件
				jarArchiver.createArchive();
			}

			System.out.println("========done========");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
